package com.content_module.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.content_module.R;
import com.content_module.R2;
import com.content_module.adapter.MainAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import component.fy.com.common_module.base.BaseActivity;
import component.fy.com.common_module.baseAdapter.OnBaseAdapterItemClick;
import component.fy.com.common_module.constant.ARouterUtils;
import component.fy.com.common_module.utils.LogUtil;
import component.fy.com.common_module.utils.ToastUtil;
import component.fy.com.common_module.widget.SpaceItemDecoration;

public class BaseAdapterLayouShowAct extends BaseActivity {
    private static final String TAG = "BaseRecyclerAdapter不同布局展示";
    @BindView(R2.id.common_smart_refresh_layout_smart)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.common_smart_refresh_layout_recycler)
    RecyclerView recyclerView;
    @BindView(R2.id.content_base_adapter_layout_show_icon)
    ImageView imageView;

    private MainAdapter adapter;
    private List<String> list = new ArrayList<>();
    private int viewType = 2;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    smartRefreshLayout.finishRefresh();
                    adapter.setData(list);
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.content_base_adapter_layout_show;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new SpaceItemDecoration(0, 0, 0, 2));
        recyclerView.setHasFixedSize(true);
        adapter = new MainAdapter(this, R.layout.content_item_main);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }

            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                setData();
            }
        });
        adapter.setOnBaseAdapterItemClick(new OnBaseAdapterItemClick() {
            @Override
            public void onItemClick(View v, int position) {
                super.onItemClick(v, position);
                ToastUtil.showToast("点击位置" + position);
            }

            @Override
            public void onErrorViewClick() {
                super.onErrorViewClick();
                adapter.setEnableShowError(false);
                imageView.setEnabled(true);
                smartRefreshLayout.autoRefresh();
            }
        });
    }

    @Override
    protected void initData() {
        list.add("测试数据1");
        list.add("测试数据2");
        handler.sendEmptyMessageDelayed(0, 3000);
    }

    @OnClick(R2.id.content_base_adapter_layout_show_icon)
    void clickImg() {
        smartRefreshLayout.autoRefresh();
    }

    private void setData() {
        viewType++;
        LogUtil.e(TAG, "测试数字=" + viewType);
        switch (viewType % 3) {
            case 0://空布局显示
                list.clear();
                break;
            case 1://正常布局显示
                list.add("测试数据1");
                list.add("测试数据2");
                break;
            case 2://错误布局显示
                list.clear();
                adapter.setEnableShowError(true);
                imageView.setEnabled(false);
                break;
        }
        handler.sendEmptyMessageDelayed(0, 3000);
    }
}
